package com.itheima.canal.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class RabbitMQConfig {
    //定义队列名称
    public static final String AD_UPDATE_QUEUE="ad_update_queue";

    //定义队列名称
    public static final String SEARCH_ADD_QUEUE="search_add_queue";

    //定义交换机
    public static final String GOODS_UP_EXCHANGE="goods_up_exchange";
    public static final String PAGE_CREATE_QUEUE ="page_create_queue" ;

    //声明队列
    @Bean
    public Queue AD_UPDATE_QUEUE(){
        return new Queue(AD_UPDATE_QUEUE);
    }

    //声明队列
    @Bean(SEARCH_ADD_QUEUE)
    public Queue SEARCH_ADD_QUEUE(){
        return new Queue(SEARCH_ADD_QUEUE);
    }

    @Bean(PAGE_CREATE_QUEUE)
    public Queue PAGE_CREATE_QUEUE(){
        return new Queue(PAGE_CREATE_QUEUE);
    }

    //声明交换机
    @Bean(GOODS_UP_EXCHANGE)
    public Exchange  GOODS_UP_EXCHANGE(){
        //使用发布订阅模式fanout
        return ExchangeBuilder.fanoutExchange(GOODS_UP_EXCHANGE).durable(true).build();
    }


    //队列绑定到交换机中
    @Bean
    public Binding AD_UPDATE_QUEUE_BINDING(@Qualifier(SEARCH_ADD_QUEUE) Queue queue,
                                           @Qualifier(GOODS_UP_EXCHANGE) Exchange exchange){
        //不适用routinkey路由key
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }

    //静态页面的队列绑定到上架交换机中
    @Bean
    public Binding PAGE_CREATE_QUEUE_BINDING(@Qualifier(PAGE_CREATE_QUEUE)Queue queue,
                                             @Qualifier(GOODS_UP_EXCHANGE)Exchange exchange){
        //不适用routinkey
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }

}
